import React, {Component} from 'react';
import {Button, Descriptions, Badge} from 'antd';
import {reqCompanyInformation} from '../../api/index'
import memoryUtils from "../../utils/memoryUtils";

/*
* 公司信息路由*/
export default class ComInformation extends Component {

    state = {
        companyInformation: [],//总的公司信息列表
        loading: false,//是否正在加载中
    }

    //异步获取公司基本信息

    getCompanyInformation = async () => {
        this.setState({loading: true})//显示loading
        //发异步ajax请求，获取数据
        const result = await reqCompanyInformation()

        this.setState({loading: false})//隐藏
        if (result.data.length === 0) {
            this.props.history.replace('/com_information_add')
        } else {
            const companyInformation = result.data[0]
            memoryUtils.points=companyInformation.points
            //更新状态
            this.setState({
                companyInformation: companyInformation,
            })
        }

    }

    //发异步ajax请求
    componentDidMount() {
        this.getCompanyInformation()
    }

    //为第一次render()准备数据
    componentWillMount() {

    }


    updateCom_information = () => {
        this.props.history.replace('/com_information_update')
    }


    render() {
        //读取状态数据
        const {companyInformation} = this.state
        const {name, describe, address, contact, contact_phone,logo, license,points, credit, status} = companyInformation
        return (
            <div>
                <Descriptions title="公司信息" bordered>
                    <Descriptions.Item label="公司名称">{name}</Descriptions.Item>
                    <Descriptions.Item label="公司地址">{address}</Descriptions.Item>
                    <Descriptions.Item label="LOGO图片"><img src={logo} alt="" width='100px'/></Descriptions.Item>
                    <Descriptions.Item label="营业执照"><img src={license} alt="" width='100px'/></Descriptions.Item>
                    <Descriptions.Item label="联系人">
                        {contact}
                    </Descriptions.Item>
                    <Descriptions.Item label="联系电话">
                        {contact_phone}
                    </Descriptions.Item>
                    <Descriptions.Item label="子弹">{points}</Descriptions.Item>
                    <Descriptions.Item label="信用分">{credit}</Descriptions.Item>
                    <Descriptions.Item label="公司介绍">
                        {describe}
                    </Descriptions.Item>
                    <Descriptions.Item label="状态">
                         <Badge status="processing" text={status}/>
                    </Descriptions.Item>
                </Descriptions>
                <br/>
                <Button type="primary" block style={{width: "50%",}} onClick={this.updateCom_information}>
                    修改公司信息
                </Button>
            </div>
        )
    }
}
